package main.parsers.utils;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.swing.ImageIcon;

import main.database.Database;
import main.parsers.AbstractParser;

public abstract class Utils extends AbstractParser
{
	
	private Utils(Database db)
	{
		super(db);
	}
	
	public static Map<String, Integer> loadReferees(boolean order)
	{
		return loadPairs("select.referees", "fullname", "referees", order);
	}
	
	public static Map<String, Integer> loadCities(boolean order)
	{
		return loadPairs("select.cities", "city", "cities", order);
	}
	
	/*public static Map<String, Integer> loadSeasons()
	{
		return loadPairs("select.seasons", "season", "seasons");
	}*/
	
	public static Map<String, Integer> loadStates(boolean order)
	{
		return loadPairs("select.states", "state", "states", order);
	}
	
	public static Map<String, Integer> loadShortStates(boolean order)
	{
		return loadPairs("select.states", "short", "states", order);
	}
	
	public static Map<String, Integer> loadViolations(boolean order)
	{
		return loadPairs("select.violations", "violation", "violations", order);
	}
	
	public static Map<String, Integer> loadRoles(boolean order)
	{
		return loadPairs("select.roles", "role", "roles", order);
	}
	
	public static Map<String, Integer> loadSides(boolean order)
	{
		return loadPairs("select.sides", "side", "sides", order);
	}
	
	public static Map<String, Integer> loadDivisions(Integer side)
	{
		Map<String, Integer> divisions = new HashMap<String, Integer>();
		
		ResultSet rs = null;
		try
		{
			if (side == null) rs = Database.getInstance().executeQuery("select.divisions", null);
			else rs = Database.getInstance().executeQuery("select.divisions.side", new Object[] {side});
			while (rs.next()) divisions.put(rs.getString("division"), rs.getInt("id"));
		} 
		catch (Exception e) 
		{
			System.err.println("Unable to get divisions from database");
			e.printStackTrace();
		}
		
		return divisions;
	}
	
	public static Map<String, Integer> loadDivisions(boolean order)
	{
		return loadPairs("select.divisions", "division", "divisions", order);
	}
	
	public static Map<String, Integer> loadCountries(boolean order)
	{
		return loadPairs("select.countries", "country", "countries", order);
	}
	
	private static Map<String, Integer> loadPairs(String queryName, String attribute, String error, boolean order)
	{
		Map<String, Integer> pairs;
		
		if (order) pairs = new LinkedHashMap<String, Integer>();
		else pairs = new HashMap<String, Integer>();
		
		ResultSet rs = null;
		try
		{
			rs = Database.getInstance().executeQuery(queryName, null);
			while (rs.next()) pairs.put(rs.getString(attribute), rs.getInt("id"));
		} 
		catch (Exception e) 
		{
			System.err.println("Unable to get " + error + " from database");
			e.printStackTrace();
		}
		finally
		{
			closeResultSet(rs);
		}
		
		return pairs;
	}
	
	/*private static Map<String, Integer> loadPairs(String queryName, String attribute, String error)
	{
		return loadPairs(queryName, attribute, error, false);
	}*/
	
	public static Map<ImageIcon, Integer> loadFlags()
	{
		Map<ImageIcon, Integer> flags = new LinkedHashMap<ImageIcon, Integer>();
		
		ResultSet rs = null;
		try
		{
			rs = Database.getInstance().executeQuery("select.countries", null);
			while (rs.next()) 
			{
				ByteArrayInputStream in = new ByteArrayInputStream(rs.getBytes("flag"));
				ObjectInputStream is = new ObjectInputStream(in);
				ImageIcon flag = (ImageIcon)is.readObject();
				flag.setDescription(rs.getString("country"));
				flags.put(flag, rs.getInt("id"));
			}
		} 
		catch (Exception e) 
		{
			System.err.println("Unable to get flags from database");
			e.printStackTrace();
		}
		finally
		{
			closeResultSet(rs);
		}
		
		return flags;
	}
	
	public static Map<String, Integer> loadClubs(boolean order)
	{
		return loadPairs("select.clubs", "name", "clubs", order);
	}
	
	public static Map<String, Integer> loadTypes(boolean order)
	{
		return loadPairs("select.types", "type", "types", order);
	}
	
	public static Map<String, Integer> loadYears(boolean order)
	{
		return loadPairs("select.years", "year", "years", order);
	}
	
	public static Map<String, Integer> loadDuties(boolean order)
	{
		return loadPairs("select.duties", "duty", "duties", order);
	}
	
	public static Map<String, Integer> loadNominations(boolean order)
	{
		return loadPairs("select.nominations", "nomination", "nominations", order);
	}
	
	public static boolean makePair(String queryName, String name, String error)
	{
		try
		{
			return Database.getInstance().executeChange(queryName, new Object[] {name}) > 0;
		}
		catch (Exception e)
		{
			System.err.println("Unable to add " + error);
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean makeViolation(String violation)
	{
		return makePair("insert.violation", violation, "violation");
	}
	
	public static boolean makeYear(String year)
	{
		return makePair("insert.year", year, "year");
	}
	
	public static boolean makeType(String type)
	{
		return makePair("insert.type", type, "type");
	}
}
